export default {
    computed: {
        user() {
            return this.$store.state.user.data
        }
    },
    data() {
        const feedbackTextValidate = (rule, value, callback) => {
            if (value === "" || value.replace(/(^\s*)|(\s*$)/g, "") === "" || (/^\s+$/gi).test(value)) {
                callback(new Error("不能为空,请正确填写"));
            } else {
                callback();
            }
        }
        return {
            feedbackColumns: [{
                'title': '序号',
                'align': 'center',
                'type': 'index',
                'width': 70
            }, {
                'title': '时间',
                'key': 'createtime',
                'align': 'center',
                'width': 100

            }, {
                'title': '反馈内容',
                'key': 'content',
                'align': 'center',
            }, {
                'title': '处理状态',
                'key': 'handle_state',
                'align': 'center',
                'width': 100

            }, {
                'title': '处理详情',
                'key': 'handle_content',
                'align': 'center',
            }, {
                'title': '操作',
                'key': 'action',
                'align': 'center',
                'width': 150

            }],
            tableData: [],
            pageNum: 1,
            pageCount: 0,
            pageLoading: false,
            modalLoading: true,
            showModal: false,
            selectFeedBack: '',
            feedbackModalForm: {
                feedbackText: ''
            },
            feedbackModalFormRules: {
                feedbackText: [{
                    required: true,
                    validator: feedbackTextValidate,
                    trigger: 'blur'
                }],
            }
        }
    },
    methods: {
        /**
         * @param {Boolean} type
         * 设置确定按钮loading动画，传True或者False
         */
        changeLoadingStatus(type) {
            if (!type) {
                this.modalLoading = false;
            }
            this.$nextTick(() => {
                this.modalLoading = true;
            });
        },
        getFeedbackList() {
            if (this.pageLoading) return
            this.pageLoading = true;
            this.http.post(this.postUrl.doGetFeedbackList, {
                token: this.user.tokenList[0],
                pageNum: this.pageNum,
                pageSize: 20,
                key_word: ''
            }).then((data) => {
                console.log(data);
                if (data.code > -1) {
                    this.pageCount = data.data.totalRow;
                    this.tableData = JSON.parse(JSON.stringify(data.data.list));
                } else {
                    this.errorHandle(data.msg)
                }
            }, () => {
                this.errorHandle()
            }).then(() => {
                this.pageLoading = false;
            })
        },
        ignoreFeedback(param) {
            this.http.post(this.postUrl.doIgnoreFeedback,{
                token:this.user.tokenList[0],
                uuid:param.uuid,
                app_user_uuid:param.app_user_uuid

            }).then((data)=>{
                if(data.code > -1){
                    this.$Notice.success({
                        title: '操作成功！',
                    })
                    this.getFeedbackList()
                }else {
                    this.errorHandle(data.msg)
                }
            }, () => {
                this.errorHandle()
            })
        },
        changePage(num) {
            this.pageNum = num;
            this.getFeedbackList();
            document.querySelector('.single-page-con').scrollTo(0, 0)

        },
        submitFeedback() {
            this.changeLoadingStatus(true);
            this.$refs.feedbackModalForm.validate(valid => {
                if (!valid) {
                    this.changeLoadingStatus(false);
                    return
                }
                this.http.post(this.postUrl.doDealFeedback, {
                    token: this.user.tokenList[0],
                    uuid: this.selectFeedBack.uuid,
                    handle_content: this.feedbackModalForm.feedbackText,
                    app_user_uuid:this.selectFeedBack.app_user_uuid
                }).then((data) => {
                    if (data.code > -1) {
                        this.showModal = false;
                        this.getFeedbackList();
                    } else {
                        this.errorHandle(data.msg)
                    }
                    this.changeLoadingStatus(false);

                }, () => {
                    this.errorHandle()
                })
            })
        }
    },
    created() {
        this.feedbackColumns.forEach(item => {
            if (item.key == 'action') {
                item.render = ((h, param) => {
                    return h('div', [
                        h('Button', {
                            class: 'table-action-box',
                            props: {
                                type: "text",
                                disabled:param.row.handle_state !== '待处理'
                            },
                            on: {
                                click: () => {
                                    this.selectFeedBack = param.row;
                                    this.showModal = true;
                                }
                            }
                        }, '回复'),
                        h(
                            "Poptip", {
                                props: {
                                    confirm: true,
                                    title: "您确定要忽略这条反馈?",
                                    transfer: true
                                },
                                on: {
                                    "on-ok": () => {
                                        this.ignoreFeedback(param.row)
                                    }
                                }
                            }, [
                                h('Button', {
                                    class: 'ignore-btn',
                                    props: {
                                        type: "text",
                                        color: 'primary',
                                        disabled:param.row.handle_state !== '待处理'
                                    },
                                    on: {
                                        click: () => {

                                        }
                                    }
                                }, '忽略')
                            ]
                        )
                    ])
                })

            }
            // item.push({
            //     'action':actionItem
            // })
        })
        if (this.user) {
            this.getFeedbackList()
        }

    },
    watch: {
        user(val, old) {
            if (val && !old) {
                this.getFeedbackList()

            }
        },
        showModal(val) {
            if (!val) {
                this.feedbackModalForm.feedbackText = '';
                this.selectFeedBack = '';
            }
        }

    }
}
